﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using WCSFContrib.ECommerceCatalog.BusinessEntities;

namespace WCSFContrib.ECommerceCatalog.User.Views
{
    public class DefaultPresenter : Presenter<IDefaultView>
    {
        private IUserController _controller;
        private string _userName;

        public string UserName
        {
            set { _userName = value; }
        }

        public DefaultPresenter([CreateNew] IUserController controller)
        {
            this._controller = controller;
            this._controller.StartUserPageFlow();
            this._controller.CurrentPage = "UserProfile";
        }

        public override void OnViewInitialized()
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);

            if (userProfile == null)
                _controller.CreateUserProfile(_userName);
        }
        
        public override void OnViewLoaded()
        {
            LoadAddressesListForCurrentUser();
            LoadPaymentsMethodsListForCurrentUser();
            LoadOrdersListForCurrentUser();
        }

        private void LoadAddressesListForCurrentUser()
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);

            if(userProfile == null)
                return;

            ICollection<Address> addressList = userProfile.Addresses;

            View.ShowAddressList(addressList);
            View.TotalAddressCount = addressList.Count;
        }

        private void LoadPaymentsMethodsListForCurrentUser()
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);

            if (userProfile == null)
                return;

            ICollection<Payment> paymentList = userProfile.PaymentsMethods;

            View.ShowPaymentsMethods(paymentList);
            View.TotalPaymentsCount = paymentList.Count;
        }

        private void LoadOrdersListForCurrentUser()
        {
            ICollection<Order> orderList = _controller.GetOrdersByUserName(_userName);

            if (orderList == null)
                return;
            
            View.ShowOrders(orderList);
            View.TotalOrdersCount = orderList.Count;
        }

        public void OnAddAddress(Address address)
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);

            if (userProfile == null)
                return;

            _controller.AddAddress(userProfile.UserProfileId, address);

            LoadAddressesListForCurrentUser();
        }
        
        public void OnEditAddress(Address address)
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);
            _controller.UpdateAddress(userProfile.UserProfileId, address);
        }

        public void OnDeleteAddress(Address address)
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);
            _controller.RemoveAddress(userProfile.UserProfileId, address.AddressId);

            //LoadAddressesListForCurrentUser();
        }

        public void OnAddPaymentMethod(Payment payment)
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);

            if (userProfile == null)
                return;

            _controller.AddPaymentMethod(userProfile.UserProfileId, payment);

            LoadPaymentsMethodsListForCurrentUser();
        }

        public void OnEditPaymentMethod(Payment payment)
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);
            _controller.UpdatePayment(userProfile.UserProfileId, payment);
        }

        public void OnDeletePaymentMethod(Payment payment)
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);
            _controller.RemovePaymentMethod(userProfile.UserProfileId, payment.PaymentId);

            //LoadPaymentsMethodsListForCurrentUser();
        }

        public void OnGoToCart()
        {
            _controller.GoCartPage();
        }
    }
}




